/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.figures.connection;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineDecoration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;

/**
 * This edge figure is painted using dashes. This is the base figure for
 * dependencies, for exempale
 */
public class DashEdgeFigure extends EdgeFigure {
	
	/**
	 * Dashes used to paint line.
	 */
	private int[] dashes =new int[10];
	
	/**
	 * 
	 */
	protected boolean arrow=true;
	
	/**
	 * Creates a new DashEdgeFigure.
	 */ 
	public DashEdgeFigure() {
		super();
		setStyle();
	}
	
	/**
	 * 
	 * 
	 * @param b 
	 */
	public DashEdgeFigure(Boolean b) {
		super();
		arrow= b;
		setStyle();
	}
	
	/**
	 * 
	 * 
	 * @param b 
	 */
	public void setArrow(Boolean b){
		arrow= b;
	}
	
	/**
	 * Creates a new DashEdgeFigure., with given parameters
	 * 
	 * @param lineWidth 
	 * @param foregroundColor 
	 * @param backgroundColor 
	 */ 
	public DashEdgeFigure(Color backgroundColor, Color foregroundColor, int lineWidth) {
		super();
		setStyle(backgroundColor, foregroundColor, lineWidth);
	}
	
	/**
	 * Sets initial Style for the figure.
	 * It does not give any special Color for Background and Foreground. This
	 * is the style used for classic arrows: ------>
	 */
	protected void setStyle() {
		PolylineDecoration dec=new PolylineDecoration();
		dec.setScale( 15, 5);
		dec.setLineWidth(1);
		if (arrow){
		this.setTargetDecoration(dec);} // arrow at target endpoint
		this.setLineStyle(Graphics.LINE_CUSTOM);  // line drawing style
		
		// set dashes
		for (int i=0;i<10;i++) {
			dashes[i]=5;
		}
		
	}
	
	/**
	 * Sets initial style for this figure.
	 * This is the style used for full arrows: ------|>
	 * 
	 * @param lineWidth the width of the link
	 * @param foregroundColor the foreground color of the link
	 * @param backgroundColor the background color of the link
	 */
	protected void setStyle(int lineWidth) {
		setStyle();
		// set link appearence
		setLineWidth(lineWidth);
	}

	/**
	 * Sets initial style for this figure.
	 * This is the style used for full arrows: ------|>
	 * 
	 * @param lineWidth the width of the link
	 * @param foregroundColor the foreground color of the link
	 * @param backgroundColor the background color of the link
	 */
	protected void setStyle(Color backgroundColor, Color foregroundColor, int lineWidth) {
		// set the target arrow style
		PolygonDecoration dec = new PolygonDecoration();
		dec.setScale( 15, 5);
		dec.setBackgroundColor( backgroundColor);
		dec.setLineWidth(1);
		if( arrow){
			setTargetDecoration(dec);
		}
		// set link appearence
		setLineWidth(lineWidth);
		setLineStyle(Graphics.LINE_CUSTOM);  // line drawing style
		setForegroundColor(foregroundColor);
		
		// set dashes
		for (int i=0;i<10;i++) {
			dashes[i]=5;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.draw2d.Shape#paintFigure(org.eclipse.draw2d.Graphics)
	 */
	/**
	 * 
	 * 
	 * @param graphics 
	 */
	@Override
	public void paintFigure(Graphics graphics) {
		graphics.pushState();
		graphics.setAntialias(SWT.ON);
		if (dashes != null) {
			graphics.setLineDash(dashes);
		}
		
		super.paintFigure(graphics);
		if (img!=null){
		super.FillFigure(graphics);}
		graphics.popState();
	}
		
}